﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace ColorBall
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            Balls = new List<ColorBall>();
            InitializeDic();

        }

        /// <summary>
        /// 
        /// </summary>
        void InitializeDic()
        {
            RedBallDic = new Dictionary<int, int>();
            for (int i = 1; i < 34; i++)
            {
                RedBallDic.Add(i, 0);
            }
            BlueBallDic = new Dictionary<int, int>();
            for (int i = 1; i < 17; i++)
            {
                BlueBallDic.Add(i, 0);
            }
            NumberRemDic = new Dictionary<int, int>();
            for (int i = 0; i < 10; i++)
            {
                NumberRemDic.Add(i, 0);
            }
        }


        /// <summary>
        /// 数据。
        /// </summary>
        List<ColorBall> Balls;

        Dictionary<int, int> RedBallDic;

        Dictionary<int, int> BlueBallDic;

        Dictionary<int, int> NumberRemDic;



        private void ToolStripReadData_Click(object sender, EventArgs e)
        {
            string filePath = Directory.GetCurrentDirectory();
            string ballDataName = filePath + "\\数据\\双色球.xls";
            DataTable DT = OpenData.GetDataTable("sheet1", ballDataName);
            foreach (DataRow row in DT.Rows)
            {
                ColorBall ball = new ColorBall(int.Parse(row.ItemArray[0].ToString()));
                ball.RedBall.Add(int.Parse(row.ItemArray[1].ToString()));
                ball.RedBall.Add(int.Parse(row.ItemArray[2].ToString()));
                ball.RedBall.Add(int.Parse(row.ItemArray[3].ToString()));
                ball.RedBall.Add(int.Parse(row.ItemArray[4].ToString()));
                ball.RedBall.Add(int.Parse(row.ItemArray[5].ToString()));
                ball.RedBall.Add(int.Parse(row.ItemArray[6].ToString()));
                ball.BlueBall = int.Parse(row.ItemArray[7].ToString());
                Balls.Add(ball);
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //string filePath = Directory.GetCurrentDirectory();          
            //FileName = filePath + "\\双色球.obj";
            //bool b=false ;
            //if (File.Exists(FileName) == true)
            //    Balls = FileOperation.OpenFromBinary(FileName, ref b) as List<ColorBall>;
            //else
            //    Balls = new List<ColorBall>();
        }

        private void ToolStripHistoryView_Click(object sender, EventArgs e)
        {
            FrmHistory frmHistory = new FrmHistory(Balls);
            frmHistory.ShowDialog();
        }



        private void BtnCal_Click(object sender, EventArgs e)
        {
            if (Balls.Count == 0) return;
            int num = 0;
            if (radioAllData.Checked) num = Balls.Count;
            else
            {
                num = int.Parse(txtNum.Text);
                if (Balls.Count < num)
                {
                    return;
                }
            }
            
            InitializeDic();

            List<ColorBall> RecentBalls = Balls.GetRange(0, num);

            if (radioAllNumber.Checked)
            {

                foreach (ColorBall ball in RecentBalls)
                {
                    foreach (int redNum in ball.RedBall)
                    {
                        RedBallDic[redNum]++;
                    }
                    BlueBallDic[ball.BlueBall]++;
                }
                CalMaxMinNumber();
            }
            else
            {
                foreach (ColorBall ball in RecentBalls)
                {
                    int rem = 0;
                    int div = 0;
                    foreach (int redNum in ball.RedBall)
                    {                        
                        div = Math.DivRem(redNum, 10, out rem);
                        NumberRemDic[rem]++;
                    }
                    div = Math.DivRem(ball.BlueBall, 10, out rem);
                    NumberRemDic[rem]++;
                }
            }
        }

        private void btnViewResult_Click(object sender, EventArgs e)
        {
            string resultStr = "";
            if (radioAllNumber.Checked)
            {
                foreach (KeyValuePair<int, int> redBall in RedBallDic)
                {
                    resultStr += "红球 " + redBall.Key.ToString() + " 出现次数: " + redBall.Value.ToString() + "\n";
                }
                foreach (KeyValuePair<int, int> blueBall in BlueBallDic)
                {
                    resultStr += "蓝球 " + blueBall.Key.ToString() + " 出现次数: " + blueBall.Value.ToString() + "\n";
                }
            }
            else
            {
                foreach (KeyValuePair<int, int> ball in NumberRemDic)
                {
                    resultStr += "余数 " + ball.Key.ToString() + " 出现次数: " + ball.Value.ToString() + "\n";
                }
            }
            FrmResult frmResult = new FrmResult(resultStr);
            frmResult.ShowIcon = false;
            frmResult.ShowDialog();
        }

        /// <summary>
        /// 计算冷门热门方法。
        /// </summary>
        void CalMaxMinNumber()
        {
            #region 计算冷门号码

            KeyValuePair<int, int> minBall = new KeyValuePair<int, int>(0, int.MinValue);
            for (int i = 0; i < 6; i++)
            {
                List<KeyValuePair<int, int>> minBalls = CalMin(minBall);
                string minRedStr = " ";
                for (int j = 0; j < minBalls.Count; j++)
                {
                    minRedStr += minBalls[j].Key.ToString() + " ";
                }
                if (i == 0) txtMinRed1.Text = minRedStr;
                else if (i == 1) txtMinRed2.Text = minRedStr;
                else if (i == 2) txtMinRed3.Text = minRedStr;
                else if (i == 3) txtMinRed4.Text = minRedStr;
                else if (i == 4) txtMinRed5.Text = minRedStr;
                else if (i == 5) txtMinRed6.Text = minRedStr;
                minBall = minBalls[0];
            }


            int minValue = int.MaxValue;
            List<int> minNums = new List<int>();
            foreach (KeyValuePair<int, int> item in BlueBallDic)
            {
                if (item.Value == minValue)
                {
                    minNums.Add(item.Key);
                    continue;
                }
                if (item.Value < minValue)
                {
                    minValue = item.Value;
                    minNums.Clear();
                    minNums.Add(item.Key);
                }
            }
            string minBlueStr = " ";
            for (int i = 0; i < minNums.Count; i++)
            {
                minBlueStr += minNums[i].ToString() + " ";
            }
            txtMinBlue.Text = minBlueStr;


            #endregion

            #region 计算热门号码

            KeyValuePair<int, int> maxBall = new KeyValuePair<int, int>(0, int.MaxValue);
            for (int i = 0; i < 6; i++)
            {
                List<KeyValuePair<int, int>> maxBalls = CalMax(maxBall);
                string maxRedStr = " ";
                for (int j = 0; j < maxBalls.Count; j++)
                {
                    maxRedStr += maxBalls[j].Key.ToString() + " ";
                }
                if (i == 0) txtMaxRed1.Text = maxRedStr;
                else if (i == 1) txtMaxRed2.Text = maxRedStr;
                else if (i == 2) txtMaxRed3.Text = maxRedStr;
                else if (i == 3) txtMaxRed4.Text = maxRedStr;
                else if (i == 4) txtMaxRed5.Text = maxRedStr;
                else if (i == 5) txtMaxRed6.Text = maxRedStr;
                maxBall = maxBalls[0];
            }


            int maxValue = int.MinValue;
            List<int> maxNums = new List<int>();
            foreach (KeyValuePair<int, int> item in BlueBallDic)
            {
                if (item.Value == maxValue)
                {
                    maxNums.Add(item.Key);
                    continue;
                }
                if (item.Value > maxValue)
                {
                    maxValue = item.Value;
                    maxNums.Clear();
                    maxNums.Add(item.Key);
                }
            }
            string maxBlueStr = " ";
            for (int i = 0; i < maxNums.Count; i++)
            {
                maxBlueStr += maxNums[i].ToString() + " ";
            }
            txtMaxBlue.Text = maxBlueStr;

            #endregion

        }


        List<KeyValuePair<int, int>> CalMin(KeyValuePair<int, int> minBall)
        {
            List<KeyValuePair<int, int>> minNums = new List<KeyValuePair<int, int>>();
            int minValue = int.MaxValue;
            foreach (KeyValuePair<int, int> item in RedBallDic)
            {
                if (item.Value == minValue)
                {
                    minNums.Add(item);
                    continue;
                }
                if (item.Value < minValue && item.Value > minBall.Value)
                {
                    minValue = item.Value;
                    minNums.Clear();
                    minNums.Add(item);
                }
            }
            return minNums;
        }


        List<KeyValuePair<int, int>> CalMax(KeyValuePair<int, int> maxBall)
        {
            List<KeyValuePair<int, int>> maxNums = new List<KeyValuePair<int, int>>();
            int maxValue = int.MinValue;
            foreach (KeyValuePair<int, int> item in RedBallDic)
            {
                if (item.Value == maxValue)
                {
                    maxNums.Add(item);
                    continue;
                }
                if (item.Value > maxValue && item.Value < maxBall.Value)
                {
                    maxValue = item.Value;
                    maxNums.Clear();
                    maxNums.Add(item);
                }
            }
            return maxNums;
        }


        private void ToolStripClearData_Click(object sender, EventArgs e)
        {
            Balls.Clear();            
        }

        private void btnRandom_Click(object sender, EventArgs e)
        {
            Random rand = new Random();
            List<int> redBalls = new List<int>();
            while (redBalls.Count != 6)
            {
                int value = rand.Next(1, 33);
                if (redBalls.Contains(value) == false) redBalls.Add(value);
            }       
            redBalls.Sort();
            txtRandRedBall1.Text = redBalls[0].ToString();
            txtRandRedBall2.Text = redBalls[1].ToString();
            txtRandRedBall3.Text = redBalls[2].ToString();
            txtRandRedBall4.Text = redBalls[3].ToString();
            txtRandRedBall5.Text = redBalls[4].ToString();
            txtRandRedBall6.Text = redBalls[5].ToString();
            txtRandBlueBall.Text = rand.Next(16).ToString();

        }

        private void radioAllData_Click(object sender, EventArgs e)
        {
            if (radioAllData.Checked == true)
            {
                //radioAllData.Checked = true;
                radioRecentData.Checked = false;
                txtNum.Enabled = false;
            }
        }

        private void radioRecentData_Click(object sender, EventArgs e)
        {
            if (radioRecentData.Checked == true)
            {
                //radioRecentData.Checked = true;
                radioAllData.Checked = false;
                txtNum.Enabled = true;
            }
        }

        private void radioAllNumber_Click(object sender, EventArgs e)
        {
            if (radioAllNumber.Checked)
            {
                radioRemainder.Checked = false;
            }
        }

        private void radioRemainder_Click(object sender, EventArgs e)
        {
            if (radioRemainder.Checked)
                radioAllNumber.Checked = false;
        }

            








    }
}
